﻿using Newtonsoft.Json;
using TCommon;
using TCommunication.Configuration.Interface;
using TCommunication.Model;

namespace TCommunication.Configuration.Impl;

public class JsonFileConfigProvider:IConfigProvider
{
    /// <summary>
    /// 读取缓存标志位
    /// </summary>
    private bool _isLoadConfig;
    /// <summary>
    /// 配置缓存
    /// </summary>
    private Dictionary<string, ConfigInfo> _configInfos = new();
    /// <summary>
    /// 配置文件路径
    /// </summary>
    // ReSharper disable once UnusedAutoPropertyAccessor.Global
    public string? FilePath { get; set; }
    /// <summary>
    /// 获取配置
    /// </summary>
    /// <param name="configName">配置名</param>
    /// <returns>获取结果</returns>
    public OperateResult<ConfigInfo> GetConfigInfo(string configName)
    {
        if (!_isLoadConfig)
        {
            var loadResult = LoadConfig();
            if (loadResult.IsFailed)
            {
                return OperateResult.CreateFailedResult<ConfigInfo>(loadResult.Message);
            }
            _isLoadConfig = true;
        }
        if (_configInfos.TryGetValue(configName, out var config))
        {
            return OperateResult.CreateSuccessResult(config);
        }
        else
        {
            return OperateResult.CreateFailedResult<ConfigInfo>($"未找到配置项{configName}");
        }
    }

    private OperateResult LoadConfig()
    {
        if (!string.IsNullOrEmpty(FilePath))
        {
            string fullPath;
            try
            {
                fullPath = Path.GetFullPath(FilePath);
            }
            catch (Exception e)
            {
                return OperateResult.CreateFailedResult<ConfigInfo>($"路径{FilePath}不合法:{e.Message}");
            }
            if (File.Exists(fullPath))
            {
                var allText = File.ReadAllText(fullPath);
                try
                {
                    //var deserializeObject = JsonConvert.DeserializeObject<Dictionary<string, ConfigInfo>>(allText);
                    //if (deserializeObject != null)
                    //{
                    //    this._configInfos = deserializeObject;
                    //    return OperateResult.CreateSuccessResult();
                    //}
                    //else
                    //{
                    //    return OperateResult.CreateFailedResult($"无法解析配置文件{fullPath}");
                    //}
                    var deserializeObject = JsonConvert.DeserializeObject<Dictionary<string, dynamic>>(allText);
                    if (deserializeObject != null)
                    {
                        //this._configInfos = deserializeObject;
                        foreach (var configItem in deserializeObject)
                        {
                            var configInfo = new ConfigInfo();
                            configInfo.Type = configItem.Value.Type;
                            configInfo.Config = configItem.Value.Config.ToString();
                            _configInfos.Add(configItem.Key,configInfo);
                        }
                        return OperateResult.CreateSuccessResult();
                    }
                    else
                    {
                        return OperateResult.CreateFailedResult($"无法解析配置文件{fullPath}");
                    }
                }
                catch (Exception e)
                {
                    return OperateResult.CreateFailedResult(
                        $"解析文件{fullPath}时发生异常:{e.Message}\r\n{e.Source}\r\n{e.StackTrace}");
                }
            }
            else
            {
                return OperateResult.CreateFailedResult($"文件{fullPath}不存在");
            }
        }
        else
        {
            return OperateResult.CreateFailedResult($"尚未指定配置文件路径");
        }
    }
}